package com.yanqu.road.server.cmd.activity.shuihu;

import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.activity.ShuiHuActivityProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.cross.battle.ShuiHuActivity;
import com.yanqu.road.server.manager.cross.battle.ShuiHuMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;

@Cmd(code = CrossProtocol.C2_SHUIHU_ACTIVITY_GET_OPENER, desc = "跨服获取开启者信息")
public class CrossGetShuiHuOpenerCmd extends CrossCommand {

    private int activityId;
    private long userId;
    private long serverId;
    private int roundNo;
    private int itemNo;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        ShuiHuActivityProto.CrossGetOpenerRespMsg reqMsg = ShuiHuActivityProto.CrossGetOpenerRespMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        userId = reqMsg.getUserId();
        serverId = reqMsg.getServerId();
        roundNo = reqMsg.getRoundNo();
        itemNo = reqMsg.getItemNo();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        ShuiHuActivity activity = ShuiHuMgr.getActivity(activityId);
        if (activity == null) {
            ret = GameErrorCode.E_SHUIHU_ACTIVITY_NO_OPEN;
            return;
        }

        ret = activity.getOpenerData(serverId, userId, roundNo, itemNo);
    }

    @Override
    public void sendData() {
        if (ret != 0) {
            ShuiHuActivityProto.GetOpenerRespMsg.Builder builder = ShuiHuActivityProto.GetOpenerRespMsg.newBuilder();
            builder.setRet(ret);
            MessageHelper.sendPacket(serverId, userId, YanQuMessageUtils.buildMessage(ClientProtocol.U_SHUIHU_ACTIVITY_GET_OPENER, builder));
        }
    }
}
